package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Product;
import com.example.demo.service.ProductMyBatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品MyBatis控制器
 */
@RestController
@RequestMapping("/api/mybatis/products")
public class ProductMyBatisController {
    
    @Autowired
    private ProductMyBatisService productMyBatisService;
    
    /**
     * 根据商品名称模糊查询
     */
    @GetMapping("/search")
    public Result<List<Product>> findByNameLike(@RequestParam String name) {
        List<Product> products = productMyBatisService.findByNameLike(name);
        return Result.success(products);
    }
    
    /**
     * 根据分类ID查询商品
     */
    @GetMapping("/category/{categoryId}")
    public Result<List<Product>> findByCategoryId(@PathVariable Long categoryId) {
        List<Product> products = productMyBatisService.findByCategoryId(categoryId);
        return Result.success(products);
    }
    
    /**
     * 根据价格范围查询商品
     */
    @GetMapping("/price-range")
    public Result<List<Product>> findByPriceRange(
            @RequestParam Double minPrice, 
            @RequestParam Double maxPrice) {
        List<Product> products = productMyBatisService.findByPriceRange(minPrice, maxPrice);
        return Result.success(products);
    }
    
    /**
     * 查询库存不足的商品
     */
    @GetMapping("/low-stock")
    public Result<List<Product>> findLowStockProducts(@RequestParam(defaultValue = "10") Integer threshold) {
        List<Product> products = productMyBatisService.findLowStockProducts(threshold);
        return Result.success(products);
    }
    
    /**
     * 统计商品总数
     */
    @GetMapping("/count")
    public Result<Long> countProducts() {
        Long count = productMyBatisService.countProducts();
        return Result.success(count);
    }
    
    /**
     * 根据状态统计商品数量
     */
    @GetMapping("/count-by-status")
    public Result<Long> countByStatus(@RequestParam String status) {
        Long count = productMyBatisService.countByStatus(status);
        return Result.success(count);
    }
    
    /**
     * 批量更新商品状态
     */
    @PutMapping("/batch-status")
    public Result<String> updateStatusBatch(@RequestBody List<Long> ids, @RequestParam String status) {
        int updated = productMyBatisService.updateStatusBatch(ids, status);
        return Result.success("成功更新 " + updated + " 个商品状态");
    }
    
    /**
     * 根据品牌查询商品
     */
    @GetMapping("/brand/{brand}")
    public Result<List<Product>> findByBrand(@PathVariable String brand) {
        List<Product> products = productMyBatisService.findByBrand(brand);
        return Result.success(products);
    }
    
    /**
     * 查询热销商品
     */
    @GetMapping("/hot")
    public Result<List<Product>> findHotProducts(@RequestParam(defaultValue = "10") Integer limit) {
        List<Product> products = productMyBatisService.findHotProducts(limit);
        return Result.success(products);
    }
}
